#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 2010, M = 1000010;

int n, m;
int h[N], e[M], ne[M], w[M], idx;
int d[N], q[N];
int dist[N];
bool st[N];

void add(int a, int b, int c) {
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
    d[b] ++ ;
}

void topsort() {
    int hh = 0, tt = -1;
    for (int i = 1; i <= n + m; ++ i)
        if (!d[i]) q[ ++ tt] = i;
    
    while (hh <= tt) {
        int t = q[hh ++ ];
        for (int i = h[t]; ~i; i = ne[i]) {
            int j = e[i];
            if ( -- d[j] == 0) q[ ++ tt] = j;
        }
    }
}

int main() {
    cin >> n >> m;
    
    memset(h, -1, sizeof h);
    for (int i = 1; i <= m; ++ i) {
        memset(st, 0, sizeof st);
        int cnt;
        scanf("%d", &cnt);
        int start = n, end = 1;
        while (cnt -- ) {
            int stop;
            scanf("%d", &stop);
            start = min(start, stop);
            end = max(end, stop);
            st[stop] = true;
        }
        
        int ver = n + i;
        for (int j = start; j <= end; ++ j) 
            if (!st[j]) add(j, ver, 0);
            else add(ver, j, 1);
    }
    
    topsort();
    
    for (int i = 1; i <= n; ++ i) dist[i] = 1;
    for (int i = 0; i < n + m; ++ i) {
        int t = q[i];
        for (int j = h[t]; ~j; j = ne[j]) {
            int k = e[j];
            dist[k] = max(dist[k], dist[t] + w[j]);
        }
    }
    
    int ans = 0;
    for (int i = 1; i <= n; ++ i) ans = max(ans, dist[i]);
    cout << ans << endl;
    
    return 0;
}